<?php

use yii\helpers\Html;
use yii\grid\GridView;
use app\models\Category;
use app\models\Product;
use yii\helpers\ArrayHelper;
use dosamigos\datepicker\DatePicker;
use dosamigos\datepicker\DateRangePicker;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $searchModel app\models\ProductSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Products';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Product', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?php
        $array=[
            ['id'=>1,'name'=>'Actived'],
            ['id'=>0,'name'=>'Not Active']
        ]
    ?>
    <?php Pjax::begin(['id'=>'gridview']);?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute' => 'name',
                'format' => 'raw',
                'value' => function ($model) {
                        return '<div>'.$model->name.'</div>';
                    },
            ],
            [
                'attribute' => 'cat_id',
                'format' => 'raw',
                'value' => function ($model) {
                        return '<div>'.Category::getCategory($model->cat_id).'</div>';
                    },
                'filter'=>ArrayHelper::map(Category::find()->all(),'id','name'),
            ],
            [
                'attribute' => 'create_date',
                'format' => 'raw',
                'value' => function ($model) {
                        return '<div>'.$model->create_date.'</div>';
                    },
                'filter'=>DatePicker::widget([
                    'model' => $searchModel,
                    'attribute' => 'create_date',
                    'clientOptions' => [
                        'autoclose' => true,
                        'format' => 'yyyy-M-d'
                    ]
                ])
            ],
            [
                'attribute' => 'update_date',
                'format' => 'raw',
                'value' => function ($model) {
                        return '<div>'.$model->update_date.'</div>';
                    },
                'filter'=>DatePicker::widget([
                        'model' => $searchModel,
                        'attribute' => 'update_date',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-M-d'
                        ]
                    ])
            ],
            [
                'attribute' => 'active',
                'format' => 'raw',
                'value' => function ($model) {
                        return '<div>'.Product::getactive($model->active).'</div>';
                    },
                'filter'=>ArrayHelper::map($array,'id','name'),
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>
<script>
    $.pjax.defaults.timeout = false;
    $.pjax.reload({container:'#gridview'});
</script>